<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php

require_once("./include/bm_config.php");
require_once("./include/const.inc.php");
require_once("./include/my_func.inc.php");
require_once("./include/funcs.php");


$is_admin = isset($_SESSION['administrator']) || isset($_SESSION[$BM_ADMIN]);
$cnt = 0;
if(!$is_admin)
{
			
			echo "<script language='javascript'>\n";
			echo "alert('不要黑偶……');\n";
			echo "window.location=\"".$BM_HOME."\";\n";
			echo "</script>";
			exit(0);
}
if (!isset($_GET['cid']) || !isset($_GET['ckind']))
{
			
			echo "<script language='javascript'>\n";
			echo "alert('参数错误~~');\n";
			echo "history.go(-1);\n";
			echo "</script>";
			exit(0);
}


	
$cid=intval($_GET['cid']);
$contest_id = $cid;
$xlskind = intval($_GET['xlskind']);
$ckind=intval($_GET['ckind']);
if($ckind == 0)
	$titstr = $xlskind ? "简表":"祥表";
else
	$titstr = "表";
$sql="SELECT `contest_title`, `contest_0_active` FROM `cr_contest` WHERE `contest_id`=$cid";
$result=mysql_query($sql) or die(mysql_error());
$rows_cnt=mysql_num_rows($result);
if ($rows_cnt>0)
{
	$row=mysql_fetch_array($result);
	$title=$row['contest_title'];
	$contest_0_active=$row['contest_0_active'];
	if($contest_0_active == 'N')
	{
			
			echo "<script language='javascript'>\n";
			echo "alert('无此数据');\n";
			echo "history.go(-1);\n";
			echo "</script>";
			mysql_free_result($result);
			exit(0);
	}
	
	
	
		ob_end_clean();
		ob_start();
		header("Content-Type: application/force-download"); 
		header('Content-Type: application/vnd.ms-excel'); 
		//header("Content-Transfer-Encoding: binary"); 
		//header("Content-type:application/vnd.ms-excel");
		//header("Content-Disposition:attachment;filename=users.xls" );
		
		
	if($ckind == 0)
		$file_name=urlencode("contest".$cid."_".$title.$sf_name."报名$titstr.xls") ;
	else
		$file_name=urlencode("contest".$cid."_".$title.$rj_name."报名$titstr.xls");
		
	if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
	{
		$file_name=iconv("UTF-8","GB2312",$file_name);
	}
	header ( "content-disposition:   attachment;   filename=".$file_name );
}
else
{
			
			echo "<script language='javascript'>\n";
			echo "alert('没有这个比赛~~');\n";
			echo "history.go(-1);\n";
			echo "</script>";
			exit(0);
}
	
mysql_free_result($result);
if($ckind == 0)
{
					if($xlskind == 0)
					{
						$titstr = $title."[".$sf_name."报名详表]";
						$biaotou = "<tr><td>序号</td><td>学校</td><td>队伍名称</td><td>领队（教练）</td><td>联系方式</td><td>身份证</td><td>学号</td><td>姓名</td><td>专业</td><td>年级</td><td>性别</td><td>赛服尺码</td></tr>";
						if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
						{
								$titstr=iconv("UTF-8","GB2312",$titstr);
								$biaotou=iconv("UTF-8","GB2312",$biaotou);
						}
						echo "<center><h3>$titstr</h3></center>";
						echo "<table border=1>";
						
						echo $biaotou;
						$sql="
						SELECT * FROM `cr_team`
						WHERE `team_cid`='$contest_id' AND `team_kind` = 0
						ORDER BY `team_school` ASC
						";
						$result=mysql_query($sql);
						while($row = mysql_fetch_array($result))
						{
					
							$cnt ++;
							$team_id = $row['team_id'];
							$team_school = SqlToHtml($row['team_school']);
							$team_name = SqlToHtml($row['team_name']);
							$team_coach = SqlToHtml($row['team_coach']);
							$team_phone = SqlToHtml($row['team_phone']);
							if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
							{
								$team_id=iconv("UTF-8","GB2312",$team_id);
								$team_school=iconv("UTF-8","GB2312",$team_school);
								$team_name=iconv("UTF-8","GB2312",$team_name);
								$team_coach=iconv("UTF-8","GB2312",$team_coach);
								$team_phone=iconv("UTF-8","GB2312",$team_phone);
							}
							echo "<tr><td rowspan = '3'>$cnt</td><td rowspan = '3'>$team_school</td><td rowspan = '3'>$team_name</td><td rowspan = '3'>$team_coach</td><td rowspan = '3'>$team_phone</td>";
							$sql="
							SELECT * FROM `cr_stu` 
							WHERE `team_id`='$team_id'
							ORDER BY `stu_123` ASC
							";
							$lcnt = 0;
							$lres = mysql_query($sql);
							while($lrow = mysql_fetch_array($lres))
							{
								$lcnt ++;
								if($lcnt > 3) break;
								$team_id = SqlToHtml($row['team_id']);
								$stu_card = SqlToHtml($lrow['stu_card']);
								$stu_number = SqlToHtml($lrow['stu_number']);
								$stu_name = SqlToHtml($lrow['stu_name']);
								$stu_major = SqlToHtml($lrow['stu_major']);
								$stu_grade = SqlToHtml($lrow['stu_grade']);
								$stu_sex = $lrow['stu_sex'] ? "女":"男";
								$stu_cloth = ($lrow['stu_cloth']);
								
								if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
								{
									$stu_card=iconv("UTF-8","GB2312",$stu_card);
									$stu_number=iconv("UTF-8","GB2312",$stu_number);
									$stu_name=iconv("UTF-8","GB2312",$stu_name);
									$stu_major=iconv("UTF-8","GB2312",$stu_major);
									$stu_grade=iconv("UTF-8","GB2312",$stu_grade);
									$stu_sex=iconv("UTF-8","GB2312",$stu_sex);
									$stu_cloth=iconv("UTF-8","GB2312",$stu_cloth);
								}
								
								echo "<td>$stu_card</td><td>$stu_number</td><td>$stu_name</td><td>$stu_major</td><td>$stu_grade</td><td>$stu_sex</td><td>$stu_cloth</td></tr>";
							}
							while($lcnt < 3)
							{
								$lcnt ++;
								$neirong = "<td>未填写</td><td>未填写</td><td>未填写</td><td>未填写</td><td>未填写</td><td>".($stu_sex ?"女":"男")."</td><td>未填写</td>";
								
								if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
								{
									$neirong=iconv("UTF-8","GB2312",$neirong);
								}
								echo $neirong;
								echo "</tr>";
							}
							mysql_free_result($lres);
						}
						
						mysql_free_result($result);
						echo "</table>";
					}
					else
					{
						$titstr = $title."[".$sf_name."报名简表]";
						$biaotou = "<tr><td>序号</td><td>队伍名称</td><td>学校</td><td>队员姓名</td><td>领队（教练）</td></tr>";
						if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
						{
								$titstr=iconv("UTF-8","GB2312",$titstr);
								$biaotou=iconv("UTF-8","GB2312",$biaotou);
						}
						echo "<center><h3>$titstr</h3></center>";
						echo "<table border=1>";
						echo $biaotou;
						$sql="
						SELECT * FROM `cr_team`
						WHERE `team_cid`='$contest_id' AND `team_kind` = 0
						ORDER BY `team_school` ASC
						";
						$result=mysql_query($sql);
						while($row = mysql_fetch_array($result))
						{
							$cnt ++;
							$team_id = $row['team_id'];
							$team_school = SqlToHtml($row['team_school']);
							$team_name = SqlToHtml($row['team_name']);
							$team_coach = SqlToHtml($row['team_coach']);
							if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
							{
								$team_school=iconv("UTF-8","GB2312",$team_school);
								$team_name=iconv("UTF-8","GB2312",$team_name);
								$team_coach=iconv("UTF-8","GB2312",$team_coach);
							}
							
							$sql="
							SELECT `stu_name` FROM `cr_stu` 
							WHERE `team_id`='$team_id'
							ORDER BY `stu_123` ASC
							";
							$lres = mysql_query($sql);
							$stu_name = "";
							for($i = 0; $i < 3; $i ++)
							{
								if($i != 0) $stu_name .= "、";
								$lrow = mysql_fetch_array($lres);
								$stu_name .= SqlToHtml($lrow['stu_name']);
							}
						
							if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
							{
								$stu_name=iconv("UTF-8","GB2312",$stu_name);
							}
							mysql_free_result($lres);
							echo "<td>$cnt</td><td>$team_name</td><td>$team_school</td><td>$stu_name</td><td>$team_coach</td></tr>";
					
						}
						mysql_free_result($result);
						echo "</table>";
					}	
}
else
{
						$cnt = 0;
						$titstr = $title."[".$rj_name."报名表]";
						$biaotou = "<tr><td>序号</td>
											<td>编号</td>
											<td>学校</td>
											<td>队伍名称</td>
											<td>领队（教练）</td>
											<td>联系方式</td>
											<td>项目类型</td>
											<td>参赛项目</td>
											<td>项目文档</td>
											<td>身份证</td>
											<td>学号</td>
											<td>姓名</td>
											<td>专业</td>
											<td>年级</td>
											<td>性别</td>
											<td>赛服尺码</td>
									</tr>";
						if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
						{
								$titstr=iconv("UTF-8","GB2312",$titstr);
								$biaotou=iconv("UTF-8","GB2312",$biaotou);
						}
						echo "<center><h3>$titstr</h3></center>";
						echo "<table border=1>";
						echo $biaotou;
						$sql="SELECT * FROM `cr_xmkind` WHERE `contest_id`= $cid ORDER BY xmkind_num ASC";
						$result=mysql_query($sql);
						$i = 0;
						while ($row=mysql_fetch_object($result))
						{
							$i ++;
							$xmkind_name[$i] = $row->xmkind_name;
						}
						mysql_free_result($result);
						
						
						$sql="
						SELECT * FROM `cr_team`
						WHERE `team_cid`='$contest_id' AND `team_kind` != 0
						ORDER BY `team_school` ASC
						";
						$result=mysql_query($sql);
						while($row = mysql_fetch_array($result))
						{
					
							$cnt ++;
							$team_id = $row['team_id'];
							$team_school = SqlToHtml($row['team_school']);
							$team_name = SqlToHtml($row['team_name']);
							$team_coach = SqlToHtml($row['team_coach']);
							$team_phone = SqlToHtml($row['team_phone']);
							$team_kind = $row['team_kind'];
							$team_upload = $row['team_upload'];
							
							$team_xmkind = $xmkind_name[$team_kind];
							$team_xmname = SqlToHtml($row['team_xmname']);
							if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
							{
								$team_id=iconv("UTF-8","GB2312",$team_id);
								$team_school=iconv("UTF-8","GB2312",$team_school);
								$team_name=iconv("UTF-8","GB2312",$team_name);
								$team_coach=iconv("UTF-8","GB2312",$team_coach);
								$team_phone=iconv("UTF-8","GB2312",$team_phone);
								$team_xmname=iconv("UTF-8","GB2312",$team_xmname);
								$team_xmkind=iconv("UTF-8","GB2312",$team_xmkind);
							}
							
							
							
							$sql="
							SELECT * FROM `cr_stu` 
							WHERE `team_id`='$team_id'
							ORDER BY `stu_id` ASC
							";
				
							$lcnt = 0;
							$lres = mysql_query($sql);
							$rows_cnt=mysql_num_rows($lres);
							
							echo "<tr><td rowspan = $rows_cnt>$cnt</td>
												<td rowspan = $rows_cnt>$team_id</td>
												<td rowspan = $rows_cnt>$team_school</td>
												<td rowspan = $rows_cnt>$team_name</td>
												<td rowspan = $rows_cnt>$team_coach</td>
												<td rowspan = $rows_cnt>$team_phone</td>
												<td rowspan = $rows_cnt>$team_xmkind</td>
												<td rowspan = $rows_cnt>$team_xmname</td>";
							if (strlen($team_upload) == 0)
							{
								$zanque = "<td rowspan = $rows_cnt>暂缺</td>";
								if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
								{
									$zanque=iconv("UTF-8","GB2312",$zanque);
								}
                	echo $zanque;
              }
           		else
           		{
								$yitijiao = "<td rowspan = $rows_cnt>已提交</td>";
								if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
								{
									$yitijiao=iconv("UTF-8","GB2312",$yitijiao);
								}
                	echo $yitijiao;
              }
	                       
							if($rows_cnt == 0)
							{
								echo "</tr>";
								continue;
							}
							//echo $rows_cnt;		
							while($lrow = mysql_fetch_array($lres))
							{
								$lcnt ++;
								//if($lcnt > 3) break;
								$team_id = SqlToHtml($row['team_id']);
								$stu_card = SqlToHtml($lrow['stu_card']);
								$stu_number = SqlToHtml($lrow['stu_number']);
								$stu_name = SqlToHtml($lrow['stu_name']);
								$stu_major = SqlToHtml($lrow['stu_major']);
								$stu_grade = SqlToHtml($lrow['stu_grade']);
								$stu_sex = $lrow['stu_sex'] ? "女":"男";
								$stu_cloth = ($lrow['stu_cloth']);
								
								if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
								{
									$stu_card=iconv("UTF-8","GB2312",$stu_card);
									$stu_number=iconv("UTF-8","GB2312",$stu_number);
									$stu_name=iconv("UTF-8","GB2312",$stu_name);
									$stu_major=iconv("UTF-8","GB2312",$stu_major);
									$stu_grade=iconv("UTF-8","GB2312",$stu_grade);
									$stu_sex=iconv("UTF-8","GB2312",$stu_sex);
									$stu_cloth=iconv("UTF-8","GB2312",$stu_cloth);
								}
								
								echo "<td>$stu_card</td>
											<td>$stu_number</td>
											<td>$stu_name</td>
											<td>$stu_major</td>
											<td>$stu_grade</td>
											<td>$stu_sex</td>
											<td>$stu_cloth</td>
									</tr>";
							}
							mysql_free_result($lres);
						}
						
						mysql_free_result($result);
						echo "</table>";
}


?>
